package pers.qianyu.month_202104.date_20210404;

import org.junit.Assert;
import org.junit.Test;

/**
 * 5722. 截断句子
 * https://leetcode-cn.com/contest/weekly-contest-235/problems/truncate-sentence/
 *
 * @author mizzle rain
 * @date 2021-04-04 11:05
 */
public class TruncateSentence {
    public String truncateSentence(String s, int k) {
        int n = s.length();
        if (n == 0) return "";
        int left = 0;
        char[] arr = s.toCharArray();
        int cnt = 0;
        while (left < n) {
            while (left < n && arr[left] != ' ') left++;
            cnt++;
            if (cnt == k) {
                return s.substring(0, left);
            }
            while (left < n && arr[left] == ' ') left++;
        }
        return s;
    }

    @Test
    public void test1() {
        String r = new TruncateSentence().truncateSentence("Hello how are you Contestant", 4);
        Assert.assertEquals("Hello how are you", r);
    }

    @Test
    public void test2() {
        String r = new TruncateSentence().truncateSentence("What is the solution to this problem", 4);
        Assert.assertEquals("What is the solution", r);
    }

    @Test
    public void test3() {
        String r = new TruncateSentence().truncateSentence("chopper is not a tanuki", 5);
        Assert.assertEquals("chopper is not a tanuki", r);
    }
}
